package com.jwthhealth_pub.wristband.setting.view;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.base.BaseResp;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.utils.CToast;
import com.jwthhealth_pub.utils.Contas;
import com.jwthhealth_pub.wristband.presenter.IWristbandSettingPresenter;
import com.jwthhealth_pub.wristband.view.WristBandSettingUserInfoActivity;
import com.suke.widget.SwitchButton;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;

/**
 * 手环设置
 * Created by yang on 2017/12/16.
 */

public class WristbandSettingActivity extends BaseActivity implements IWristbandSettingPresenter.view {

    private final String TAG = makeLogTag(WristbandSettingActivity.class);

    @BindView(R.id.title_layout)
    TitleLayout titleLayout;
    @BindView(R.id.switch_button)
    SwitchButton switchButton;
    @BindView(R.id.tv_setting_targetstep)
    TextView btnGuideTargetStep;
    @BindView(R.id.tv_setting_desc_heart)
    TextView tvSettingDescHeart;
    @BindView(R.id.tv_setting_value_heart)
    TextView tvSettingValueHeart;
    @BindView(R.id.tv_setting_desc_pressure_blood)
    TextView tvSettingDescPressureBlood;
    @BindView(R.id.tv_setting_value_pressure_blood)
    TextView tvSettingValuePressureBlood;
    @BindView(R.id.tv_setting_desc_systolic_blood)
    TextView tvSettingDescSystolicBlood;
    @BindView(R.id.tv_setting_value_systolic_blood)
    TextView tvSettingValueSystolicBlood;
    @BindView(R.id.tv_setting_desc_blood)
    TextView tvSettingDescBlood;
    @BindView(R.id.tv_setting_value_fatigue)
    TextView tvSettingValueFatigue;
//    @BindView(R.id.tv_setting_value_blood)
//    TextView tvSettingValueBlood;

    private List<String> targetStepList;
    private String mTargetSetp;

    private String mWarnValue;

    private boolean isWarnEnable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settingwristband);
        ButterKnife.bind(this);
        switchButtonCheckStats();

        loadSaveSetting();

        setWarnEnable(false);
    }

    @Override
    protected void title() {
        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    //开关按钮的操作
    public void switchButtonCheckStats() {
        switchButton.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                if (isChecked) {
//                    CToast.show(getBaseContext(), "选中了");
//                    showAlertDialog();
                    setWarnEnable(true);
                } else {
//                    CToast.show(getBaseContext(), "没有选中");
                    setWarnEnable(false);
                }
            }
        });

    }

    //这是手环预警的弹出框
    private void showAlertDialog() {

        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setView(LayoutInflater.from(this).inflate(R.layout.alert_show_wristaband, null));
        dialog.show();
        dialog.getWindow().setContentView(R.layout.alert_show_wristaband);
        TextView btnPositive = (TextView) dialog.findViewById(R.id.btn_ok);
        TextView btnNegative = (TextView) dialog.findViewById(R.id.btn_cancel);
        btnPositive.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
//                CToast.show(getBaseContext(), "ok");
                switchButton.setChecked(true);

                setWarnEnable(true);

                dialog.dismiss();
            }
        });
        btnNegative.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                switchButton.setChecked(false);
                CToast.show(getBaseContext(), "cancel");
                dialog.dismiss();
            }
        });
    }

    private void setWarnEnable(boolean b) {
        int color;
        if (b) {
            color = getResources().getColor(R.color.textColorBlack);
        } else {
            color = getResources().getColor(R.color.textColorGray_light);
        }

        tvSettingDescBlood.setTextColor(color);
        tvSettingDescHeart.setTextColor(color);
        tvSettingDescPressureBlood.setTextColor(color);
        tvSettingDescSystolicBlood.setTextColor(color);
        tvSettingValueSystolicBlood.setTextColor(color);
        tvSettingValueFatigue.setTextColor(color);
        tvSettingValueHeart.setTextColor(color);
        tvSettingValuePressureBlood.setTextColor(color);

        isWarnEnable = b;
    }

    @OnClick({R.id.tv_wristband_userinfo, R.id.tv_wristband_healthtarget, R.id.tv_wristband_connnection,
            R.id.btn_settins_pressureblood, R.id.btn_settins_systolicblood, R.id.btn_settins_fatigue,
            R.id.btn_setting_heart, R.id.tv_wristband_reset, R.id.tv_wristband_addwatch})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_wristband_userinfo:
                startActivity(new Intent(this, WristBandSettingUserInfoActivity.class));
                break;
            case R.id.tv_wristband_healthtarget:
//                startActivity(new Intent(this, WristBandSettingHealthTargetActivity.class));
                showStepTargetDialog();
                break;
            case R.id.tv_wristband_connnection:
                startActivity(new Intent(this, WristBandConnManaActivity.class));
                break;
            case R.id.btn_settins_pressureblood:
                if (isWarnEnable)
                    showWarnDialog(1);
                break;
            case R.id.btn_settins_systolicblood:
                if (isWarnEnable)
                    showWarnDialog(2);
                break;
            case R.id.btn_settins_fatigue:
                if (isWarnEnable)
                    showWarnDialog(3);
                break;
            case R.id.btn_setting_heart:
                if (isWarnEnable)
                    showWarnDialog(0);
                break;
            case R.id.tv_wristband_reset:
                //恢复出厂设置
                showResteDialog();
                break;
            case R.id.tv_wristband_addwatch:
                Toast.makeText(this, "添加监护对象", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void showStepTargetDialog() {
        targetStepList = new ArrayList<>();

        for (int i = 0; i < 35; i++) {
            targetStepList.add((1000 + 1000 * i) + "步");
        }

        final BottomSheetDialog shopbottomSheetDialog = new BottomSheetDialog(this);
        View view = View.inflate(this, R.layout.item_guide_target_step, null);
        shopbottomSheetDialog.setContentView(view);
        TextView completeBtn = view.findViewById(R.id.tv_complete);
        ImageView closeBtn = view.findViewById(R.id.btn_close);

        LoopView loopView = view.findViewById(R.id.loopView);
        loopView.setNotLoop();
        loopView.setItems(targetStepList);
//        loopView.setInitPosition(20);

        String origanlStep = targetStepList.get(19);
        if (origanlStep != null) {
            mTargetSetp = origanlStep;
        }

        loopView.setListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(int index) {
                mTargetSetp = targetStepList.get(index);
            }
        });

        completeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //已经选择步数
                btnGuideTargetStep.setText(mTargetSetp);
                Contas.putName(getBaseContext(), "mTargetSetp", mTargetSetp);

                LoginModel.DataBean userData = Contas.getObject(WristbandSettingActivity.this, LoginModel.DataBean.class);
                if (userData == null) return;
                String id = userData.getId();
                if (id == null) return;
                String token = userData.getAndroidtoken();
                if (token == null) return;


                Call<BaseResp> call = ApiHelper.setGoal(mTargetSetp, id, token);
                call.enqueue(new Callback<BaseResp>() {
                    @Override
                    public void onResponse(Call<BaseResp> call, Response<BaseResp> response) {
                        BaseResp body = response.body();
                        if (body == null) return;

                        String msg = body.getMsg();
                        if (msg == null) return;

                        Log.d(TAG, msg);

                    }

                    @Override
                    public void onFailure(Call<BaseResp> call, Throwable t) {
                        Log.e(TAG, t.toString());
                    }
                });

                shopbottomSheetDialog.dismiss();
            }
        });

        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shopbottomSheetDialog.dismiss();
            }
        });

        shopbottomSheetDialog.show();
    }

    private void showResteDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog resetDialog = builder.create();
        View view = View.inflate(this, R.layout.item_setting_reset, null);
        resetDialog.setView(view);

        view.findViewById(R.id.btn_setting_reset_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(WristbandSettingActivity.this, "已恢复出厂设置", Toast.LENGTH_SHORT).show();
                resetDialog.dismiss();
            }
        });

        view.findViewById(R.id.btn_setting_reset_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resetDialog.dismiss();
            }
        });

        resetDialog.show();
    }

    /**
     * 0 心率 1 舒张压 2 收缩压 3 疲劳度
     *
     * @param type
     */
    private void showWarnDialog(final int type) {

        final ArrayList<String> warnParamList = new ArrayList<>();
        mWarnValue = new String();

        switch (type) {
            case 0:
                for (int i = 0; i < 120; i++) {
                    warnParamList.add((100 + i) + "次");
                }
                break;
            case 1:
                for (int i = 0; i < 80; i++) {
                    warnParamList.add((100 + i) + "mmHg");
                }
                break;
            case 2:
                for (int i = 0; i < 100; i++) {
                    warnParamList.add((140 + i) + "mmHg");
                }
                break;
            case 3:
                warnParamList.add("轻度疲劳");
                warnParamList.add("过度疲劳");
                break;
            default:
                break;
        }

        if (warnParamList.get(0) != null)
            mWarnValue = warnParamList.get(0);

        final BottomSheetDialog shopbottomSheetDialog = new BottomSheetDialog(this);
        View view = View.inflate(this, R.layout.item_guide_target_step, null);
        shopbottomSheetDialog.setContentView(view);
        TextView completeBtn = view.findViewById(R.id.tv_complete);
        ImageView closeBtn = view.findViewById(R.id.btn_close);

        LoopView loopView = view.findViewById(R.id.loopView);
        loopView.setNotLoop();
        loopView.setItems(warnParamList);

        if (type == 0) {

            loopView.setInitPosition(30);
            mWarnValue = warnParamList.get(30);
        }

        loopView.setListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(int index) {
//                mTargetSetp = targetStepList.get(index);
                mWarnValue = warnParamList.get(index);
            }
        });

        completeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (type == 0)
                    tvSettingValueHeart.setText(mWarnValue);
                else if (type == 1)
                    tvSettingValuePressureBlood.setText(mWarnValue);
                else if (type == 2)
                    tvSettingValueSystolicBlood.setText(mWarnValue);
                else if (type == 3)
                    tvSettingValueFatigue.setText(mWarnValue);

                shopbottomSheetDialog.dismiss();
            }
        });

        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shopbottomSheetDialog.dismiss();
            }
        });

        shopbottomSheetDialog.show();
    }

    @Override
    public void loadSaveSetting() {
//        Contas.putName(getBaseContext(), "mTargetSetp", mTargetSetp);
        String stepGoal = Contas.getName(getBaseContext(), mTargetSetp);
        if (stepGoal != null)
            btnGuideTargetStep.setText(stepGoal);
//        WristBandSettingModule module = Contas.getObject(this, WristBandSettingModule.class);
//        if (module != null) {
//            WristBandSettingModule.DataBean data = module.getData();
//            if (data == null) return;
//
//            WristBandSettingModule.DataBean.BiaozhunBean biaozhun = data.getBiaozhun();
//            if (biaozhun == null) return;
//
//            String walk_goal = biaozhun.getWalk_goal();
//            btnGuideTargetStep.setText(walk_goal);
//        }
    }
}
